<?php

namespace app\apixcx\controller;

use EasyWeChat\Factory;
use app\admin\model\User as UserModel;
use app\admin\model\Info as InfoModel;

// use XuTL\QCloud\Tim\Tim;

class User
{
    protected $UserModel;
    protected $InfoModel;
    // protected $tmCient;

    public function __construct()
    {
        $this->UserModel = new UserModel();
        $this->InfoModel = new InfoModel();
        // $this->tmCient = new Tim(
        //     '1400159606',
        //     '36862',
        //     'MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgM7Z+H5RzaQ14361EN6F6Yb1ZC2igeVfyNuQYORpB7RChRANCAAQ3UrWdjEMxYe2RL6mtCzegF5dHTKghf/Ti6yczgdCg+geUnxM7XvPs3Od4Um6I+dPBO+yuC2MrveX8W/D5KOwD',
        //     'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEN1K1nYxDMWHtkS+prQs3oBeXR0yoIX/04usnM4HQoPoHlJ8TO17z7NzneFJuiPnTwTvsrgtjK73l/Fvw+SjsAw==',
        //     'admincomma'
        // );
    }

    // 登录
    public function login($code)
    {
        $app_id = getheaders()['App-Id'];
        $InfoRes = $this->InfoModel->where('wechatappid', $app_id)->find();
        if ($InfoRes['wechatsecret'] || $InfoRes) {
            $config = [
                'app_id' => $InfoRes['wechatappid'],
                'secret' => $InfoRes['wechatsecret'],
            ];
        } else {
            $config = [
                'app_id' => config('wechat.mini_program.default.app_id'),
                'secret' => config('wechat.mini_program.default.secret'),
            ];
        }
        $weChatApp = Factory::miniProgram($config);
        $weChatRes = $weChatApp->auth->session($code);
        $data = [
            'openid' => $weChatRes['openid'],
        ];
        $userRes = $this->UserModel->where($data)->find();
        if (!$userRes) {
            $userRes = $this->UserModel->create($data);
        }
        $userRes['session_key'] = $weChatRes['session_key'];

        //生成token
        $token = settoken();
        $userRes['token'] = $token;
        cache('token'.$userRes->id, $token, 1800);
        cache('token_admin'.$userRes->id, $token, 1800);

        if (!$userRes['name']) {
            $userRes['name'] = '陌生人'.$userRes['id'];
        }
        if (!$userRes['image']) {
            $userRes['image'] = '';
        }
        unset($userRes['openid']);
        //获取用户TIM登录usersig
        // $signature = $this->tmCient->getLoginSignature('comma_tm_'.$userRes['id']);
        // $userRes['Identifier'] = 'comma_tm_'.$userRes['id'];
        // $userRes['userSig'] = $signature;
        // $userRes['nickName'] = $userRes['name'];

        //更新TIM用户资料
        // $this->tmCient->accountImport('comma_tm_'.$userRes['id'], $userRes['name'], $userRes['image']);

        return $userRes;
    }

    //解密手机号
    // public function getPhone($session,$iv,$encryptData){
    //     $token = getheaders()['Http-Token'];
    //     if(!$token || $token != cache('token'.$uid)){
    //         return -100;
    //     }
    //     $weChatApp = Factory::miniProgram($config);
    //     $decryptedData = $weChatApp->encryptor->decryptData($session, $iv, $encryptData);
    //     return $decryptedData;
    // }

    //更新资料用户名与手机号
    public function updataNamePhone($uid, $name, $image)
    {
        $token = getheaders()['Http-Token'];
        if (!$token || $token != cache('token'.$uid)) {
            return -100;
        }
        $userRes = $this->UserModel->get($uid);
        if ($userRes) {
            $userRes->name = $name;
            $userRes->image = $image;
            $res = $userRes->save();
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    //更新资料用户名与手机号
    public function updateFormId($uid, $formid)
    {
        $token = getheaders()['Http-Token'];
        if (!$token || $token != cache('token'.$uid)) {
            return -100;
        }
        $userRes = $this->UserModel->get($uid);
        if ($userRes) {
            $userRes->formid = $formid;
            $res = $userRes->save();
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }
}
